

set more off

***Set working directory - end users will want to change this
global workdir "E:\SRTR\HCV\replication kit\"

set scheme s1mono
set graphics on
graph set window fontface "Times New Roman" 
graph set eps fontface Times

cd "$workdir\output\"

cap log close
log using "$workdir\logs\regressions - CITS.log", text replace


****************************************
****** Regressions *********************
****************************************

use "$workdir\outputdata\estimation sample.dta", clear


********
** WL additions, MELD scores

preserve

drop if wl_org=="KI"
drop if !inrange(can_listing_year,2005,2019)
drop if missing(can_listing_opo_id)

gen wladd = 1 if !missing(wl_org)

collapse (sum) wladd (mean) initial_meld highmeld middlemeld lowmeld, by(can_dgn_hep can_listing_opo_id can_listing_year)

fillin can_dgn_hep can_listing_opo_id can_listing_year

foreach x in wladd {
	replace `x' = 0 if missing(`x')
	gen ln`x' = ln(1+`x')
}

gen year = can_listing_year

gen daa = 0
replace daa = 1 if year>=2014

gen daa_hcv = 0
replace daa_hcv = 1 if daa==1 & can_dgn_hep==1

gen timesincedaa = 0
replace timesincedaa = year - 2013 if year>2013

**for ym:
*gen timesincedaa = 0
*replace timesincedaa = ym - 647 if ym>647 // 647 is December 2013

*initialize outreg file
global outregfilename wladditions_dsayear
reg wladd year
outreg2 using $outregfilename.xls, excel stats(coef se) keep(year) dec(4) pdec(4) replace


*************************************
**** Appendix Table 1, column 3 *****
*************************************

foreach x in wladd {
 
	reghdfe ln`x' year c.year#i.can_dgn_hep daa daa_hcv timesincedaa c.timesincedaa#i.can_dgn_hep, absorb(i.can_listing_opo_id##i.can_dgn_hep) cluster(i.can_listing_opo_id#i.can_dgn_hep)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append

	sum `x' if e(sample) & can_dgn_hep==1
	sum `x' if e(sample) & can_dgn_hep==1 & inrange(year,2005,2013)
	sum `x' if e(sample) & can_dgn_hep==0
	sum `x' if e(sample) & can_dgn_hep==0 & inrange(year,2005,2013)
	
}

*********************************

*************************************
**** Appendix Table 2, column 1 *****
*************************************

foreach x in initial_meld {
 
	reghdfe `x' year c.year#i.can_dgn_hep daa daa_hcv timesincedaa c.timesincedaa#i.can_dgn_hep, absorb(i.can_listing_opo_id##i.can_dgn_hep) cluster(i.can_listing_opo_id#i.can_dgn_hep)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append

	sum `x' if e(sample) & can_dgn_hep==1
	sum `x' if e(sample) & can_dgn_hep==1 & inrange(year,2005,2013)
	sum `x' if e(sample) & can_dgn_hep==0
	sum `x' if e(sample) & can_dgn_hep==0 & inrange(year,2005,2013)

}

***********************************

restore

*********************************************************************************************************


preserve

drop if wl_org=="KI"
drop if !inrange(can_listing_year,2005,2019)
drop if missing(can_listing_opo_id)

gen wladd = 1 if !missing(wl_org)

gen nonhcvdiag = 0 if !missing(wl_org) & can_dgn_hep==0
replace nonhcvdiag = 1 if !missing(wl_org) & inlist(can_dgn,4214)
replace nonhcvdiag = 1 if !missing(wl_org) & inlist(can_dgn,4208,4213) & can_bmi>=30
replace nonhcvdiag = 1 if !missing(wl_org) & inlist(can_dgn2,4214) & inlist(can_dgn,4400,4401)
replace nonhcvdiag = 2 if !missing(wl_org) & inlist(can_dgn,4215,4217)
replace nonhcvdiag = 2 if !missing(wl_org) & inlist(can_dgn2,4215,4217) & inlist(can_dgn,4400,4401)

drop if can_dgn_hep==1

collapse (sum) wladd , by(nonhcvdiag can_listing_opo_id can_listing_year)

fillin nonhcvdiag can_listing_opo_id can_listing_year

drop if missing(nonhcvdiag)

** for ym:
*collapse (sum) wladd (mean) initial_meld highmeld middlemeld lowmeld, by(can_dgn_hep can_listing_opo_id can_listing_ym)
*fillin can_dgn_hep can_listing_opo_id can_listing_ym

foreach x in wladd {
	replace `x' = 0 if missing(`x')
	gen ln`x' = ln(1+`x')
}

gen year = can_listing_year

gen year_nash = 0
replace year_nash = can_listing_year if nonhcvdiag==1

gen year_ald = 0
replace year_ald = can_listing_year if nonhcvdiag==2

/**for ym:
gen date = dofm(can_listing_ym)
gen can_listing_year = year(date)
drop date
gen ym = can_listing_ym
gen year = can_listing_year
format ym %tm
*/

gen daa = 0
replace daa = 1 if year>=2014

gen daa_nash = 0
replace daa_nash = 1 if year>=2014 & nonhcvdiag==1

gen daa_ald = 0
replace daa_ald = 1 if year>=2014 & nonhcvdiag==2

gen timesincedaa = 0
replace timesincedaa = year - 2013 if year>2013

gen timesincedaa_nash = 0
replace timesincedaa_nash = year - 2013 if year>2013 & nonhcvdiag==1

gen timesincedaa_ald = 0
replace timesincedaa_ald = year - 2013 if year>2013 & nonhcvdiag==2


*************************************
**** Appendix Table 4 ***************
*************************************

foreach x in wladd {
 
	reghdfe ln`x' timesincedaa_nash timesincedaa_ald timesincedaa daa_nash daa_ald daa year_nash year_ald year, absorb(can_listing_opo_id#nonhcvdiag) cluster(can_listing_opo_id)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) keep(timesincedaa_nash timesincedaa_ald timesincedaa daa_nash daa_ald daa year_nash year_ald year) dec(4) pdec(4) append
	
}

***********************************

restore

*********************************************************************************************************

preserve

drop if wl_org=="KI"
drop if !inrange(can_rem_year,2005,2019)
drop if missing(can_listing_opo_id)
drop if missing(can_dgn_hep)


collapse (sum) toosickdied improved ddtx ldtx tx (mean) last_meld_* , by(can_dgn_hep wl_org can_listing_opo_id can_rem_year)


fillin can_dgn_hep wl_org can_listing_opo_id can_rem_year

foreach x in toosickdied improved ddtx ldtx tx {
	replace `x' = 0 if missing(`x')
}

gen year = can_rem_year

gen wl_year = can_rem_year
merge m:1 can_listing_opo_id wl_year wl_org using "$workdir/outputdata/WL composition by OPO and year.dta" // this brings in size and composition variables
drop if _merge!=3

foreach x in toosickdied improved tx ddtx ldtx {
	gen `x'_pct = `x'/numonwlhcv if can_dgn_hep==1	
	replace `x'_pct = `x'/numonwlnonhcv if can_dgn_hep==0
	gen ln`x' = ln(1+`x')
}	

drop if missing(can_listing_opo_id)

gen daa = 0
replace daa = 1 if year>=2014

gen daa_hcv = 0
replace daa_hcv = 1 if daa==1 & can_dgn_hep==1

gen timesincedaa = 0
replace timesincedaa = year - 2013 if year>2013


*initialize outreg file
global outregfilename wlremovals_dsayear
reg tx year
outreg2 using $outregfilename.xls, excel stats(coef se) keep(year) dec(4) pdec(4) replace


********************************************
**** Appendix Table 1, columns 1 and 2 *****
**** .. and Appendix Table 2, column 2 *****
********************************************


foreach x in tx {

	reghdfe `x'_pct year c.year#i.can_dgn_hep daa daa_hcv timesincedaa c.timesincedaa#i.can_dgn_hep, absorb(i.can_listing_opo_id##i.can_dgn_hep) cluster(i.can_listing_opo_id#i.can_dgn_hep)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append
	
	sum `x'_pct if e(sample) & can_dgn_hep==1
	sum `x'_pct if e(sample) & can_dgn_hep==1 & inrange(year,2005,2013)
	sum `x'_pct if e(sample) & can_dgn_hep==0
	sum `x'_pct if e(sample) & can_dgn_hep==0 & inrange(year,2005,2013)

	
	reghdfe ln`x' year c.year#i.can_dgn_hep daa daa_hcv timesincedaa c.timesincedaa#i.can_dgn_hep, absorb(i.can_listing_opo_id##i.can_dgn_hep) cluster(i.can_listing_opo_id#i.can_dgn_hep)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append
	
	sum `x' if e(sample) & can_dgn_hep==1
	sum `x' if e(sample) & can_dgn_hep==1 & inrange(year,2005,2013)
	sum `x' if e(sample) & can_dgn_hep==0
	sum `x' if e(sample) & can_dgn_hep==0 & inrange(year,2005,2013)
	
	reghdfe last_meld_`x' year c.year#i.can_dgn_hep daa daa_hcv timesincedaa c.timesincedaa#i.can_dgn_hep, absorb(i.can_listing_opo_id##i.can_dgn_hep) cluster(i.can_listing_opo_id#i.can_dgn_hep)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append
	
	sum last_meld_`x' if e(sample) & can_dgn_hep==1
	sum last_meld_`x' if e(sample) & can_dgn_hep==1 & inrange(year,2005,2013)
	sum last_meld_`x' if e(sample) & can_dgn_hep==0
	sum last_meld_`x' if e(sample) & can_dgn_hep==0 & inrange(year,2005,2013)
}

*********************************


*************************************
**** Appendix Table 3 ***************
*************************************

foreach x in toosickdied improved {

	reghdfe `x'_pct year c.year#i.can_dgn_hep daa daa_hcv timesincedaa c.timesincedaa#i.can_dgn_hep, absorb(i.can_listing_opo_id##i.can_dgn_hep) cluster(i.can_listing_opo_id#i.can_dgn_hep)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append
	
	sum `x'_pct if e(sample) & can_dgn_hep==1
	sum `x'_pct if e(sample) & can_dgn_hep==1 & inrange(year,2005,2013)
	sum `x'_pct if e(sample) & can_dgn_hep==0
	sum `x'_pct if e(sample) & can_dgn_hep==0 & inrange(year,2005,2013)

	
	reghdfe ln`x' year c.year#i.can_dgn_hep daa daa_hcv timesincedaa c.timesincedaa#i.can_dgn_hep, absorb(i.can_listing_opo_id##i.can_dgn_hep) cluster(i.can_listing_opo_id#i.can_dgn_hep)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append
	
	sum `x' if e(sample) & can_dgn_hep==1
	sum `x' if e(sample) & can_dgn_hep==1 & inrange(year,2005,2013)
	sum `x' if e(sample) & can_dgn_hep==0
	sum `x' if e(sample) & can_dgn_hep==0 & inrange(year,2005,2013)
	
}

************************************

restore


cap log close
